//
//  SetController.m
//  HB
//
//  Created by 陶秋安 on 16/3/10.
//  Copyright © 2016年 孙智慧. All rights reserved.
//

#import "SetController.h"
#import "LoginOperation.h"
#import "LoginPage.h"

@implementation SetController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableSet.delegate=self;
    
    self.tableSet.dataSource=self;
    
    self.tableSet.scrollEnabled=NO;
    
    self.btnLoginout.layer.cornerRadius=2.0f;
    
    self.btnLoginout.clipsToBounds=YES;
}

-(void)setNavigationLeft:(NSString *)imageName sel:(SEL)sel
{
    
}

- (void)setNavigationRight:(NSString *)imageName sel:(SEL)sel
{
    
}

- (void)setNavigationTitleImage:(NSString *)imageName
{
    self.navigationItem.title = @"设置";
}

-(void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    strCatchSize = [Common getCacheSize];
    
    UITableViewCell *cell = [self.tableSet  cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];
    
    if(cell != nil)
    {
        ((UILabel *) cell.accessoryView).text=strCatchSize;
    }
}


#pragma mark 设置表格有几行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}

#pragma mark 表格绑定数据
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * const kCellIdentifier = @"mysetrow";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
    
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier];
    }
    
    if(indexPath.row==0)
    {
        cell.textLabel.text = @"推送设置";
        
        UISwitch *myswitch=[[UISwitch alloc]init];
        
        myswitch.frame= CGRectMake(0.0f, 0.0f, 50.0f, 20.0f);
        
        NSString *switchValue=  [Common getUserDefaults:@"switchValue"];
        
        if(switchValue == nil || [switchValue isEqualToString:@"1"] )
        {
            myswitch.on = YES;//设置初始为ON的一边
        }
        else
        {
            myswitch.on = NO;//设置初始为ON的一边
        }
        
        [myswitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        
        cell.accessoryView=myswitch;
    }
    else  if(indexPath.row==1)
    {
        UILabel *lblCatchSize=[[UILabel alloc]init];
        
        lblCatchSize.frame= CGRectMake(0.0f, 0.0f, 50.0f, 20.0f);
        
        [lblCatchSize setText:[NSString stringWithString:strCatchSize]];
        
        cell.accessoryView=lblCatchSize;
        
        cell.textLabel.text = @"清除缓存";
    }
    else  if(indexPath.row==2)
    {
        cell.textLabel.text = @"版本更新";
        
        cell.accessoryType= UITableViewCellAccessoryNone;
    }
    else  if(indexPath.row==3)
    {
        cell.textLabel.text = @"关于我们";
        
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    return cell;
    
}

-(void) opSuccessForPost:(id)data
{
    
}

-(void) switchAction :(id)sender
{
    
    UISwitch *mySwitch=(UISwitch *)sender;
    
    if(mySwitch.isOn)
    {
        [[FxGlobal global].push registerPush];
        
        [Common setUserDefaults:@"switchValue" andValue:@"1"];
    }
    else
    {
        [[FxGlobal global].push unregisterPush];
        
        [Common setUserDefaults:@"switchValue" andValue:@"0"];
    }
    
}

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if(indexPath.row==1)
    {
        [self waitingFor:@"正在清理" andMethod:@selector(startClearCatch) onTarget:self];

        UITableViewCell *cell = [self.tableSet cellForRowAtIndexPath:indexPath];
        
        ((UILabel *) cell.accessoryView).text= @"0.0M";
    }
}

-(void)startClearCatch
{
     _locked =@"n";
    
    [Common clearCache:self sel:@selector(clearCacheSuccess)];
    
    while ([_locked isEqualToString:@"y"])
    {
 
    }
    
}

-(void)clearCacheSuccess
{
    _locked = @"y";
    
    NSLog(@"清理成功");
}

- (IBAction)btnLoginoutClick:(id)sender
{
    NSString *djID=  [Common getUserDefaults:@"djID"];
 
    DBHelper *db =[DBHelper sharedMyClass];
    
    [db upData:djID andIsLogin:NO];
    
    LoginPage *loginPage=[[LoginPage alloc] init];
    
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginPage];
    
    self.view.window.rootViewController = nav;
}
@end
